package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.*;
import com.yidu.logisticssystem.entity.vo.CarVo;
import com.yidu.logisticssystem.entity.vo.CarriageVo;
import com.yidu.logisticssystem.entity.vo.StateVo;
import com.yidu.logisticssystem.service.*;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 运输单(LogisticsCarriage)表控制层
 *
 * @author lzx
 * @since 2021-03-12 15:07:52
 */
@RestController
@RequestMapping("logisticsCarriage")
public class LogisticsCarriageController {
    /**
     * 服务对象
     */
    @Resource
    private LogisticsCarriageService logisticsCarriageService;
    /**
     * 司机表服务对象
     */
    @Resource
    private LogisticsCardriverService logisticsCardriverService;
    /**
     * 车辆表服务对象
     */
    @Resource
    private LogisticsCarService logisticsCarService;
    /**
     * 服务对象
     */
    @Resource
    private LogisticsBatchService logisticsBatchService;
    /**
     * 服务对象
     */
    @Resource
    private LogisticsStateService logisticsStateService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsCarriage selectOne(Integer id) {
        return this.logisticsCarriageService.queryById(id);
    }




    @GetMapping("insert")
    public BaseMessage insert(StateVo vo, HttpServletRequest session){
       Integer count= logisticsCarriageService.fahuo(vo,session);
        return count!=null?BaseMessage.succes():BaseMessage.fail();
    }


    @GetMapping("insertps")
    public BaseMessage insertps(StateVo vo, HttpServletRequest session){
        Integer count= logisticsCarriageService.paisong(vo,session);
        return count!=null?BaseMessage.succes():BaseMessage.fail();
    }




    @GetMapping("insertkdyps")
    public BaseMessage insertkdyps(StateVo vo, HttpServletRequest session){
        Integer count= logisticsCarriageService.insertkdysd(vo,session);
        return count!=null?BaseMessage.succes():BaseMessage.fail();
    }


    /**
     * 根据登陆进来的司机ID查找
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询司机的运单信息")
    @GetMapping("queryAll2")
    public Map<String, Object> findAll( int page, int limit,CarriageVo vo,HttpSession session) {
        //得到员工（司机）ID
        LogisticsStaff logisticsStaff=(LogisticsStaff) session.getAttribute("staff");
        vo.setCarSjid(logisticsStaff.getStaffId());
        //将参数传入到分页助手
        PageHelper.startPage(page, limit);
        Map<String, Object> map = new HashMap<>();
        List<CarriageVo> list = logisticsCarriageService.queryAll2(vo);
        //分页查询到集合之后，将集合数据封装到分页助手的PageInfo对象中
        PageInfo pageInfo = new PageInfo(list);
        //查询行数
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list); // 这里可以直接写list
        map.put("count", pageInfo.getTotal()); // 总行数自动生成
        return map;
    }


    @ApiOperation(value = "修改到达时间,司机状态，车辆状态")
    @PostMapping("update")
    public BaseMessage update(String batchNum,String id,String carNum,String carSjid,String carriageDestination,String carriageStart){
        Integer count = logisticsCarriageService.updateTime(batchNum,id,carNum,carSjid,carriageDestination,carriageStart);
        return count!=null?BaseMessage.succes():BaseMessage.fail();
    }
}
